package com.yjhb.android.sbz.fragment;

import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yjhb.android.sbz.R;
import com.yjhb.android.sbz.tool.EventMsg;
import com.yjhb.android.sbz.imps.BaseTextChangedListener;
import com.yjhb.android.sbz.tool.LoadingDialogTool;
import com.jojo.android.zxlib.base.BaseFragment;
import com.jojo.android.zxlib.base.BaseRecyclerAdapter;
import com.jojo.android.zxlib.base.mvp.BasePresenter;
import com.jojo.android.zxlib.base.mvp.BaseView;
import com.jojo.android.zxlib.tool.ZxNetTool;
import com.jojo.android.zxlib.tool.ZxToast;
import com.jojo.android.zxlib.view.EmptyRecyclerView;
import com.jojo.android.zxlib.view.dialog.ILoading;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 创建者 zx
 * 创建时间 2020/12/15 14:58
 */
public abstract class BaseListFragment<P extends BasePresenter, RA extends BaseRecyclerAdapter, DATA> extends BaseFragment<P>
        implements OnRefreshLoadmoreListener, BaseRecyclerAdapter.ItemClickListener, BaseView {
    private final String TAG = "BaseListFragment";

    @BindView(R.id.recyclerView)
    protected EmptyRecyclerView recyclerView;
    @BindView(R.id.smartRefreshLayout)
    protected SmartRefreshLayout smartRefreshLayout;
    @BindView(R.id.top_tips_tv)
    protected TextView tips_tv;
    @BindView(R.id.search_et)
    protected EditText search_et;
    @BindView(R.id.search_layout)
    protected LinearLayout search_layout;
    @BindView(R.id.layout_empty)
    protected View layout_empty;
    @BindView(R.id.content_layout)
    protected LinearLayout content_layout;
    @BindView(R.id.clean_iv)
    ImageView clean_iv;

    protected ILoading loadingDialog;
    protected RA adapter;

    protected final int size = 25;
    protected int current = 1;

    protected ArrayList<DATA> dataSet = new ArrayList<>();

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_list_base;
    }

    @Override
    protected void initUI() {
        recyclerView.setEmptyView(layout_empty);
        recyclerView.setAdapter(adapter = getAdapter());
        if (adapter != null)
            adapter.setData(dataSet);
        loadingDialog = LoadingDialogTool.createDialog(this);
        CharSequence tips = getTips();
        if (TextUtils.isEmpty(tips)) {
            tips_tv.setVisibility(View.GONE);
        } else {
            tips_tv.setVisibility(View.VISIBLE);
            tips_tv.setText(tips);
        }
    }


    @Override
    public void onLoadmore(RefreshLayout refreshlayout) {
        current++;
        onLoadData(size, current);
    }

    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        current = 1;
        onLoadData(size, current);
    }

    @Override
    protected void initEvent() {
        EventBus.getDefault().register(this);
        smartRefreshLayout.setOnRefreshLoadmoreListener(this);
        if (adapter != null)
            adapter.setOnItemClick(this);
        search_et.addTextChangedListener(new BaseTextChangedListener() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s == null || s.length() < 1) {
                    clean_iv.setVisibility(View.INVISIBLE);
                } else {
                    clean_iv.setVisibility(View.VISIBLE);
                }
            }
        });
        search_et.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    current = 1;
                    onLoadData(size, current);
                    return true;
                }
                return false;
            }
        });
    }

    @Override
    public void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMsg(EventMsg msg) {

    }

    @Override
    protected void initData() {
        loadingDialog.setTipsText("加载中");
        loadingDialog.show();
        current = 1;
        onLoadData(size, current);
    }

    @Override
    public void onFailure(int code, String msg) {
        loadingDialog.cancel();
        smartRefreshLayout.finishLoadmore();
        smartRefreshLayout.finishRefresh();
        if (!ZxNetTool.isNetworkAvailable(getContext())) {
            ZxToast.showINFOBottomToast("请检查网络哦~~");
            return;
        }
        if (code != -1) {
            ZxToast.showINFOBottomToast(msg);
        }
    }

    @OnClick(R.id.clean_iv)
    void clean_iv() {
        search_et.setText("");
    }

    protected abstract RA getAdapter();

    protected abstract CharSequence getTips();


    protected abstract void onLoadData(int size, int current);
}
